<?php
#!/usr/local/php/bin/php -q

//set_time_limit(0);
//error_reporting(E_ALL);
error_reporting(0);
class MySQL {
	var $dbhost = "localhost";
	var $dbuser = "root";
	var $dbpw = "root";
	var $pconnect = 0;
	var $halt = 0;
	var $version = '';
	var $querynum = 0;
	var $link = null;
	var $lastid = 0;

	function MySQL($dbname) {
		$this->connect($dbname);
	}

	function connect($dbname = 'eventlog') {
		$func = empty($pconnect) ? 'mysql_connect' : 'mysql_pconnect';
		if(!$this->link = @$func($this->dbhost, $this->dbuser, $this->dbpw, 1)) {
			$halt && $this->halt('Can not connect to MySQL server');
		} else {
			if($this->version() > '4.1') {
				mysql_query("SET utf8", $this->link);
			}
			$dbname && @mysql_select_db($dbname, $this->link);
		}

		mysql_query("SET NAMES 'utf8';", $this->link);
	}

	function select_db($dbname) {
		return mysql_select_db($dbname, $this->link);
	}

	function FetchArray($query, $result_type = MYSQL_ASSOC) {
		return mysql_fetch_array($query, $result_type);
	}

	function fetch_first($sql) {
		return $this->fetch_array($this->query($sql));
	}

	function result_first($sql) {
		return $this->result($this->query($sql), 0);
	}

	function Query($sql, $type = '') {

		global $debug, $discuz_starttime, $sqldebug, $sqlspenttimes;


		if(!($query = mysql_query($sql, $this->link))) {
			if(in_array($this->errno(), array(2006, 2013)) && substr($type, 0, 5) != 'RETRY') {
				$this->close();
				require DISCUZ_ROOT.'./config.inc.php';
				$this->connect($dbhost, $dbuser, $dbpw, $dbname, $pconnect, true, $dbcharset);
				$this->query($sql, 'RETRY'.$type);
			} elseif($type != 'SILENT' && substr($type, 5) != 'SILENT') {
				$this->halt('MySQL Query Error', $sql);
			}
		}

		$this->querynum++;
		return $query;
	}

	function Exec($sql, $type = '') {

		global $debug, $discuz_starttime, $sqldebug, $sqlspenttimes;


		if(!($query = mysql_query($sql, $this->link))) {
			if(in_array($this->errno(), array(2006, 2013)) && substr($type, 0, 5) != 'RETRY') {
				$this->close();
				require DISCUZ_ROOT.'./config.inc.php';
				$this->connect($dbhost, $dbuser, $dbpw, $dbname, $pconnect, true, $dbcharset);
				$this->query($sql, 'RETRY'.$type);
			} elseif($type != 'SILENT' && substr($type, 5) != 'SILENT') {
				$this->halt('MySQL Query Error', $sql);
			}
		}

		$this->querynum++;
		$this->lastid = mysql_insert_id($this->link);
		return $query;
	}
	
	function GetLastID() {
		return $this->lastid;
	}

	function affected_rows() {
		return mysql_affected_rows($this->link);
	}

	function error() {
		return (($this->link) ? mysql_error($this->link) : mysql_error());
	}

	function errno() {
		return intval(($this->link) ? mysql_errno($this->link) : mysql_errno());
	}

	function result($query, $row = 0) {
		$query = @mysql_result($query, $row);
		return $query;
	}

	function RecordCount($sql) {
		$query = mysql_fetch_array($this->Query($sql));
		return $query[0];
	}

	function RecordID($sql) {
		$query = mysql_fetch_array($this->Query($sql));
		return $query[0];
	}

	function RowsNum($sql) {
		return mysql_num_rows($this->Query($sql));
	}

	function num_fields($query) {
		return mysql_num_fields($query);
	}

	function free_result($query) {
		return mysql_free_result($query);
	}

	function insert_id() {
		return ($id = mysql_insert_id($this->link)) >= 0 ? $id : $this->result($this->query("SELECT last_insert_id()"), 0);
	}

	function fetch_row($query) {
		$query = mysql_fetch_row($this->Query($query));
		return $query;
	}

	function fetch_fields($query) {
		return mysql_fetch_field($query);
	}

	function version() {
		if(empty($this->version)) {
			$this->version = mysql_get_server_info($this->link);
		}
		return $this->version;
	}

	function close() {
		return mysql_close($this->link);
	}

	function halt($message = '', $sql = '') {
		define('CACHE_FORBIDDEN', TRUE);
		$err = $this->error();
		$err = "$err !!!!! $message [$sql] !!!!! \r\n";
		error_log($err, 3, "/var/log/php_errors.log");
	}
}

//  // server independent functions for handling the database  //  // database connection

class SQLite{
    function __construct($file) {
        try {
            $this->Connection=new PDO('sqlite:'.$file);
        } catch(PDOException $e) {
            exit('{"success":false,"msg":"'.$e.'"}');
        }
    }

    function __destruct() {
    	//sqlite3_close($this->Connection);
        $this->Connection = null;
    }

    function Query($SQL) {
        return $this->Connection->Query($SQL);
    }

    function Exec($SQL) {
        return $this->Connection->exec($SQL);
    }

    function RecordArray($SQL) {
        return @$this->Query($SQL)->fetchAll();
    }

    function RecordCount($SQL) {
        return @$this->Query($SQL)->fetchColumn();//count($this->RecordArray($SQL));
    }

    function RecordLastID() {
        return $this->Connection->lastInsertId();
    }
}

Class CheckClass{
 //站点检测函数应用
    public function test_url($url,$type='ipv6'){
   		$curl = curl_init ( $url );
		curl_setopt ( $curl, CURLOPT_NOBODY, true );
		curl_setopt ( $curl, CURLOPT_TIMEOUT, 5 );
		$result = curl_exec ( $curl );
		$found = false;
		//var_dump($result);
		if ($result !== false) {
			$statusCode = curl_getinfo ( $curl, CURLINFO_HTTP_CODE );
			//echo $statusCode;
			if ($statusCode == 200||$statusCode == 302) {
				$found = true;
			}
		}
		curl_close ( $curl );
		$data['ipv4_status']=0;
		$data['ipv6_status']=0;
		$data['web']=$url;
		$data['ipv6']="";
		$data['ipv4']="";
		if($found==true){
			$data['ipv4_status']=1;
			if($type=='ipv6'){//只有检测ipv6时才进行v6检测
				$data['ipv6']=$this->gethostbyname6($url);
				if($data['ipv6']!=false)
				{
					$data['ipv6_status']=1;
				}
			}
			
		}
		return $data;
    }
    
	//升级检测相关
	public function gethostbyname6($host, $try_a = false) {
		// get AAAA record for $host
		// if $try_a is true, if AAAA fails, it tries for A
		// the first match found is returned
		// otherwise returns false
		$dns = $this->gethostbynamel6 ( $host, $try_a );
		if ($dns == false) {
			return false;
		} else {
			return $dns [0];
		}
	}
	
	public function gethostbynamel6($host, $try_a = false) {
		// get AAAA records for $host,
		// if $try_a is true, if AAAA fails, it tries for A
		// results are returned in an array of ips found matching type
		// otherwise returns false
		$dns6 = dns_get_record ( $host, DNS_AAAA );
		if ($try_a == true) {
			$dns4 = dns_get_record ( $host, DNS_A );
			$dns = array_merge ( $dns4, $dns6 );
		} else {
			$dns = $dns6;
		}
		$ip6 = array ();
		$ip4 = array ();
		foreach ( $dns as $record ) {
			if ($record ["type"] == "A") {
				$ip4 [] = $record ["ip"];
			}
			if ($record ["type"] == "AAAA") {
				$ip6 [] = $record ["ipv6"];
			}
		}
		if (count ( $ip6 ) < 1) {
			if ($try_a == true) {
				if (count ( $ip4 ) < 1) {
					return false;
				} else {
					return $ip4;
				}
			} else {
				return false;
			}
		} else {
			return $ip6;
		}
	}
}
$DB = new MySQL('sitesproxy');
$CKCLASS=new CheckClass();
$re = $DB->Query("select * from sites_info");

while ($r = $DB->FetchArray($re)) {
	//$ipv4=$r['visit_type'].'://'.$r['visit_domain'];
    //$ipv6=$r['visit_type'].'://'.$r['pre_v6'].".".$r['main_domain'];
	$ipv4=$r['visit_domain'];
    $ipv6=$r['pre_v6'].".".$r['main_domain'];
    $data_ipv4=$CKCLASS->test_url($ipv4,'ipv4');
    $data_ipv6=$CKCLASS->test_url($ipv6,'ipv6');
    $sql="update sites_info set source_status=".$data_ipv4['ipv4_status'].',ipv6_status='.$data_ipv6['ipv6_status']." where sites_id=".$r['sites_id'];
    $DB->Exec($sql);
}

?>
